package cn.hh.org.controller;

import cn.hh.basic.exception.BusinessException;
import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.ExcelUtil;
import cn.hh.basic.util.PageList;
import cn.hh.org.domain.Shop;
import cn.hh.org.domain.ShopAuditLog;
import cn.hh.org.query.ShopQuery;
import cn.hh.org.service.ShopService;
import cn.hh.org.vo.ShopEchartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺接口")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 接口：查询单个对象
     *
     * @param id
     * @return
     */
    //http://localhost:8080/shop/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     *
     * @return
     */
    //http://localhost:8080/shop + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    /**
     * 接口：分页查询
     *
     * @param query
     * @return
     */
    //http://localhost:8080/shop + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/shop/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     *
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/shop + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {//添加
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }


    /**
     * 接口：批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            //shopService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    /**
     * 接口：店铺入驻
     *
     * @param
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, "入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口: 驳回店铺
     *
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, "入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：通过店铺
     *
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核日志")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, "入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 导出店铺信息
     */
    @GetMapping("/export/excel")
//    @ApiOperation(value = "店铺审核日志")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all, null, "店铺信息", Shop.class, "店铺信息表.xlsx", response);
    }

    /**
     *
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart(value = "file", required = true) MultipartFile file) {
        // 1.使用easyPoi解析Excel,解析成java对象
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 2.做批量添加
        shops.forEach(System.out::println);
    }


    /**
     * 得到店铺报表数据
     */
    @GetMapping("/echarts")
    public List<ShopEchartVo> getShopState() {
        return shopService.getShopState();
    }
}